﻿<UserControl x:Class="PDFControl" x:Name="pc1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:ProgettoSicurezza.Client"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <UserControl.Resources>
        <local:BooleanInverter x:Key="BoolInv"/>
        <BooleanToVisibilityConverter x:Key="bool2vis"/>
    </UserControl.Resources>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <GroupBox Header="Received Documents" Margin="3" Grid.Column="0">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>


                <DataGrid Name="dgDocuments" AutoGenerateColumns="False" SelectionMode="Single" CanUserAddRows="False" CanUserDeleteRows="False" IsReadOnly="True" VerticalScrollBarVisibility="Auto" SelectionChanged="ReceivedDocuments_SelectionChanged">
                    <DataGrid.Columns>
                        <DataGridTemplateColumn Header="User Name" IsReadOnly="True">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Username}" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="5 2"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>

                        <DataGridTemplateColumn Header="Title" IsReadOnly="True" Width="*">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Title}" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="5 2"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>

                        <DataGridTemplateColumn Header="File Size" IsReadOnly="True">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="5 2">
                                        <TextBlock Text="{Binding Size}"/>
                                        <TextBlock Text=" kb"/>
                                    </StackPanel>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>

                        <DataGridTemplateColumn Header="Signed" IsReadOnly="True">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="5 2">
                                        <Image x:Name="IsSignedPic" Margin="3" Source="Resources\V.png" Height="15" Width="15">
                                            <Image.Style>
                                                <Style TargetType="Image">
                                                    <Style.Triggers>
                                                        <DataTrigger Binding="{Binding Path=IsSigned}" Value="False">
                                                            <Setter Property="Visibility" Value="Collapsed"/>
                                                        </DataTrigger>
                                                        <DataTrigger Binding="{Binding Path=IsSigned}" Value="True">
                                                            <Setter Property="Visibility" Value="Visible"/>
                                                        </DataTrigger>
                                                    </Style.Triggers>
                                                </Style>
                                            </Image.Style>
                                        </Image>
                                        <Image Margin="2" Source="Resources\X.png" Height="15" Width="15">
                                            <Image.Style>
                                                <Style TargetType="Image">
                                                    <Style.Triggers>
                                                        <DataTrigger Binding="{Binding Path=IsSigned}" Value="True">
                                                            <Setter Property="Visibility" Value="Collapsed"/>
                                                        </DataTrigger>
                                                        <DataTrigger Binding="{Binding Path=IsSigned}" Value="False">
                                                            <Setter Property="Visibility" Value="Visible"/>
                                                        </DataTrigger>
                                                    </Style.Triggers>
                                                </Style>
                                            </Image.Style>
                                        </Image>
                                    </StackPanel>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>

                        <DataGridTemplateColumn Header="Encrypted" IsReadOnly="True">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="5 2">
                                        <Image x:Name="IsSignedPic" Margin="3" Source="Resources\V.png" Height="15" Width="15">
                                            <Image.Style>
                                                <Style TargetType="Image">
                                                    <Style.Triggers>
                                                        <DataTrigger Binding="{Binding Path=IsEncrypted}" Value="False">
                                                            <Setter Property="Visibility" Value="Collapsed"/>
                                                        </DataTrigger>
                                                        <DataTrigger Binding="{Binding Path=IsEncrypted}" Value="True">
                                                            <Setter Property="Visibility" Value="Visible"/>
                                                        </DataTrigger>
                                                    </Style.Triggers>
                                                </Style>
                                            </Image.Style>
                                        </Image>
                                        <Image Margin="2" Source="Resources\X.png" Height="15" Width="15">
                                            <Image.Style>
                                                <Style TargetType="Image">
                                                    <Style.Triggers>
                                                        <DataTrigger Binding="{Binding Path=IsEncrypted}" Value="True">
                                                            <Setter Property="Visibility" Value="Collapsed"/>
                                                        </DataTrigger>
                                                        <DataTrigger Binding="{Binding Path=IsEncrypted}" Value="False">
                                                            <Setter Property="Visibility" Value="Visible"/>
                                                        </DataTrigger>
                                                    </Style.Triggers>
                                                </Style>
                                            </Image.Style>
                                        </Image>
                                    </StackPanel>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                    </DataGrid.Columns>
                </DataGrid>

                <GroupBox Margin="3" Header="Operations" Grid.Row="1">
                    <Grid >
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>

                        
                        <Button Grid.Row="0" Margin="3" Height="35" Content="Check signature" IsEnabled="{Binding ElementName=pc1, Path=IsSignedDocumentSelected}" Click="CheckSignature_Click"/>
                        <TextBlock Margin="3" Grid.Row="0" Grid.Column="1" VerticalAlignment="Center" TextWrapping="Wrap" Text="Checks if the signature on the document is valid"/>

                        <Button Margin="3" Grid.Row="1" Height="35" Content="View selected document" IsEnabled="{Binding ElementName=pc1, Path=IsDocumentSelected}" Click="ViewDocument_Click"/>
                        <TextBlock Margin="3" Grid.Row="1" Grid.Column="1" VerticalAlignment="Center" TextWrapping="Wrap" Text="Selected document will be opened with associated PDF viewer. If the document is encrypted, it will be decoded with your private key before opening it."/>

                        <Button Name="btnSaveUntouched" Grid.Row="2" Margin="3" Height="35" Content="Save copy (untouched)..." IsEnabled="{Binding ElementName=pc1, Path=IsDocumentSelected}" Click="SaveDocument_Click"/>
                        <TextBlock Margin="3" Grid.Row="2" Grid.Column="1" VerticalAlignment="Center" TextWrapping="Wrap" Text="A copy of the selected document will be saved to disk. No alteration will be done on it."/>

                        <Button Name="btnSaveDecrypted" Grid.Row="3" Margin="3" Height="35" Content="Decrypt and save copy..." IsEnabled="{Binding ElementName=pc1, Path=IsEncryptedDocumentSelected}" Click="SaveDocument_Click"/>
                        <TextBlock Margin="3" Grid.Row="3" Grid.Column="1" VerticalAlignment="Center" TextWrapping="Wrap" Text="A decrypted copy of the selected document will be saved to disk."/>
                    </Grid>
                </GroupBox>
            </Grid>

        </GroupBox>


        <Grid Grid.Column="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1.5*"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>

            <GroupBox Header="My documents" Margin="3" Grid.RowSpan="1">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>

                    <DataGrid Name="dgMyDocuments" AutoGenerateColumns="False" SelectionMode="Single" CanUserAddRows="False" CanUserDeleteRows="False" IsReadOnly="True" VerticalScrollBarVisibility="Auto" SelectionChanged="MyDocuments_SelectionChanged">
                        <DataGrid.Columns>
                            <DataGridTemplateColumn Header="User Name" IsReadOnly="True">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Username}" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="5 2"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>

                            <DataGridTemplateColumn Header="Title" IsReadOnly="True" Width="*">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Title}" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="5 2"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>

                            <DataGridTemplateColumn Header="File Size" IsReadOnly="True">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="5 2">
                                            <TextBlock Text="{Binding Size}"/>
                                            <TextBlock Text=" kb"/>
                                        </StackPanel>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                        </DataGrid.Columns>
                    </DataGrid>

                    <StackPanel Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="Center">
                        <Button Content="Add document..." Height="30" Margin="3" Click="AddDocument_Click"/>
                        <Button Content="Delete document" Margin="3" Click="DeleteDocument_Click" IsEnabled="{Binding ElementName=pc1, Path=IsMyDocumentSelected}"/>
                        <StackPanel Margin="0" IsEnabled="{Binding ElementName=pc1, Path=CanSign}">
                            <Button Content="Save signed copy to dsik..." Height="30" Margin="3" Click="SignAndSave_Click" IsEnabled="{Binding ElementName=pc1, Path=IsMyDocumentSelected}"/>
                        </StackPanel>
                    </StackPanel>
                </Grid>
            </GroupBox>

            <GroupBox Header="Sign, encrypt and Send PDF" Margin="3" Grid.Row="1">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>

                    <DataGrid Name="dgClients" AutoGenerateColumns="False" SelectionMode="Single" CanUserAddRows="False" CanUserDeleteRows="False" IsReadOnly="True" VerticalScrollBarVisibility="Auto" SelectionChanged="Clients_SelectionChanged">
                        <DataGrid.Columns>
                            <DataGridTemplateColumn Header="User Name" IsReadOnly="True">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Username}" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="5 2"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>

                            <DataGridTemplateColumn Header="Endpoint Address" IsReadOnly="True" Width="*">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding EndpointAddress}" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="5 2"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                        </DataGrid.Columns>
                    </DataGrid>

                    <DockPanel Grid.Row="1" Grid.Column="1">
                        <Button DockPanel.Dock="Left" Content="Scan online clients" Height="25" Margin="3" IsEnabled="{Binding ElementName=pc1, Path=Searching, Converter={StaticResource BoolInv}}" Click="buttonSearch_Click"/>
                        <ProgressBar Margin="3" IsIndeterminate="True" Visibility="{Binding ElementName=pc1, Path=SearchProgressVisibility}"/>
                    </DockPanel>

                    <!--<StackPanel Orientation="Horizontal" Grid.Row="2" Grid.ColumnSpan="2" HorizontalAlignment="Center">

                        <Button Height="30" Content="Sign and send" Click="SignAndSend_Click" Margin="3" IsEnabled="{Binding ElementName=pc1, Path=IsReadyToSend}"/>
                        <Button Content="Encrypt and send" Click="EncryptAndSend_Click" Margin="3" IsEnabled="{Binding ElementName=pc1, Path=IsReadyToSend}"/>
                        <Button Content="Sign, encrypt and send" Click="SignEncryptAndSend_Click" Margin="3" IsEnabled="{Binding ElementName=pc1, Path=IsReadyToSend}"/>
                    </StackPanel>-->


                    <GroupBox Header="Send options" Margin="3" Grid.Row="2" >
                        <DockPanel>

                            <GroupBox Header="Method" Margin="3" DockPanel.Dock="Left">
                                <StackPanel Orientation="Vertical" Margin="3">
                                    <CheckBox Content="Sign document" Name="cbSign" IsEnabled="{Binding ElementName=pc1, Path=CanSign}"/>
                                    <CheckBox Content="Encrypt document" Name="cbEncrypt" IsEnabled="{Binding ElementName=pc1, Path=CanEncrypt}"/>
                                </StackPanel>
                            </GroupBox>
                            <GroupBox Header="Encryption Algorithm" Margin="3" IsEnabled="{Binding ElementName=cbEncrypt, Path=IsChecked}" DockPanel.Dock="Left">
                                <StackPanel Orientation="Vertical" Margin="3">
                                    <RadioButton Content="AES 128" Name="radioAES128" IsChecked="True"/>
                                    <RadioButton Content="AES 256" Name="radioAES256"/>
                                </StackPanel>
                            </GroupBox>
                            <Button Content="Send" Click="Send_Click" Height="45" IsEnabled="{Binding ElementName=pc1, Path=IsReadyToSend}"/>
                        </DockPanel>
                    </GroupBox>

                </Grid>
            </GroupBox>
        </Grid>
    </Grid>
</UserControl>
